package com.mc.moring.util;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomTarget;
import com.mc.moring.widge.GlideCircleTransform;
import com.mc.moring.widge.GlideRoundTransform;
import com.mc.moring.widge.RoundedCornersTransform;

import java.io.File;


public class GlideImageLoadUtils {
    public static GlideImageLoadUtils glideImageLoadUtils;
    public Drawable GlidePlaceHolderDrawable = null;
    public Drawable GlideErrorDrawable = null;


    public static GlideImageLoadUtils getInstance() {
        if (glideImageLoadUtils == null) {
            synchronized (GlideImageLoadUtils.class) {
                if (glideImageLoadUtils == null) {
                    glideImageLoadUtils = new GlideImageLoadUtils();
                }
            }
        }
        return glideImageLoadUtils;
    }

    /**
     **/
    public void displayImageAsBitmap(RequestManager glide, ImageView imageView, String requestUrl) {
        displayImageAsBitmap(getRequestBuilderBitmap(glide, requestUrl), imageView, null, null, null, null);
    }


    /**
     **/
    public void displayImageAsBitmap(RequestManager glide, ImageView imageView, String requestUrl, Transformation transformation) {
        displayImageAsBitmap(getRequestBuilderBitmap(glide, requestUrl), imageView, transformation, null, null, null);
    }

    /**
     **/
    public void displayImageAsBitmap(RequestManager glide, ImageView imageView, String requestUrl, Transformation transformation, RequestListener<Bitmap> bitmapRequestListener) {
        displayImageAsBitmap(getRequestBuilderBitmap(glide, requestUrl), imageView, transformation, null, null, bitmapRequestListener);
    }

    /**
     **/
    public void displayImageAsBitmap(RequestManager glide, String requestUrl, CustomTarget<Bitmap> bitmapCustomTarget, RequestListener<Bitmap> bitmapRequestListener) {
        displayImageAsBitmap(getRequestBuilderBitmap(glide, requestUrl), null, null, null, bitmapCustomTarget, bitmapRequestListener);
    }


    /**
     **/
    public void displayImageAsBitmap(RequestManager glide, String requestUrl, CustomTarget<Bitmap> bitmapCustomTarget) {
        displayImageAsBitmap(getRequestBuilderBitmap(glide, requestUrl), null, null, null, bitmapCustomTarget, null);
    }

    /**
     **/
    public void displayImageAsBitmap(RequestManager glide, ImageView imageView, String requestUrl, RequestOptions requestOptions) {
        displayImageAsBitmap(getRequestBuilderBitmap(glide, requestUrl), imageView, null, requestOptions, null, null);
    }


    /**
     **/
    public void displayImageAsBitmap(RequestManager glide, ImageView imageView, String requestUrl, RequestOptions requestOptions, Transformation transformation) {
        displayImageAsBitmap(getRequestBuilderBitmap(glide, requestUrl), imageView, transformation, requestOptions, null, null);
    }


    /**
     **/
    public void displayImage(RequestManager glide, ImageView imageView, String requestUrl) {
        displayImage(getRequestBuilderDrawable(glide, requestUrl), imageView, null, null, null, null);
    }

    /**
     **/
    public void displayImage(RequestManager glide, ImageView imageView, File imageFile) {
        displayImage(getRequestBuilderDrawable(glide, imageFile), imageView, null, null, null, null);
    }

    /**
     **/
    public void displayCirCularImage(RequestManager glide, ImageView imageView, String requestUrl) {
        displayImage(getRequestBuilderDrawable(glide, requestUrl), imageView, new GlideCircleTransform(), null, null, null);
    }

    /**
     **/
    public void displayCornerImage(RequestManager glide, ImageView imageView, String requestUrl, int cornerdp) {
        displayImage(getRequestBuilderDrawable(glide, requestUrl), imageView, new GlideRoundTransform(cornerdp), null, null, null);
    }

    /**
     **/
    public void displayRoundedCornerImage(Fragment fragment, ImageView imageView, String requestUrl, boolean needLeftTop, boolean needRightTop, boolean needLeftBottom, boolean needRightBottom, int cornerdp) {
        RoundedCornersTransform roundedCornersTransform = new RoundedCornersTransform(fragment, needLeftTop, needRightTop, needLeftBottom, needRightBottom, cornerdp);
        displayImageAsBitmap(Glide.with(fragment), imageView, requestUrl, roundedCornersTransform);
    }


    /**
     **/
    public void displayRoundedCornerImage(Activity activity, ImageView imageView, String requestUrl, boolean needLeftTop, boolean needRightTop, boolean needLeftBottom, boolean needRightBottom, int cornerdp) {
        RoundedCornersTransform roundedCornersTransform = new RoundedCornersTransform(activity, needLeftTop, needRightTop, needLeftBottom, needRightBottom, cornerdp);
        displayImageAsBitmap(Glide.with(activity), imageView, requestUrl, roundedCornersTransform);
    }

    /**
     **/
    public void displayImage(RequestManager glide, ImageView imageView, String requestUrl, Transformation transformation) {
        displayImage(getRequestBuilderDrawable(glide, requestUrl), imageView, transformation, null, null, null);
    }


    /**
     **/
    public void displayLocalImage(RequestManager glide, ImageView imageView, int requestUrl, Transformation transformation) {
        displayImage(getRequestBuilderDrawable(glide, requestUrl), imageView, transformation, null, null, null);
    }

    /**
     **/
    public void displayLocalImage(RequestManager glide, ImageView imageView, String requestUrl, RequestOptions requestOptions) {
        displayImage(getRequestBuilderDrawable(glide, requestUrl), imageView, null, requestOptions, null, null);
    }

    /**
     **/
    public void displayLocalImage(RequestManager glide, ImageView imageView, Uri uri, RequestOptions requestOptions) {
        displayImage(getRequestBuilderDrawable(glide, uri), imageView, null, requestOptions, null, null);
    }

    /**
     **/
    public void displayImage(RequestManager glide, ImageView imageView, String requestUrl, RequestListener<Drawable> drawableRequestListener) {
        displayImage(getRequestBuilderDrawable(glide, requestUrl), imageView, null, null, null, drawableRequestListener);
    }


    /**
     **/
    public void displayImage(RequestManager glide, ImageView imageView, String requestUrl, Transformation transformation, RequestListener<Drawable> drawableRequestListener) {
        displayImage(getRequestBuilderDrawable(glide, requestUrl), imageView, transformation, null, null, drawableRequestListener);
    }


    /**
     **/
    public void displayImage(RequestManager glide, String requestUrl, Transformation transformation, CustomTarget<Drawable> drawableCustomTarget) {
        displayImage(getRequestBuilderDrawable(glide, requestUrl), null, transformation, null, drawableCustomTarget, null);
    }

    /**
     **/
    public void displayImage(RequestManager glide, String requestUrl, Transformation transformation, CustomTarget<Drawable> drawableCustomTarget, RequestListener<Drawable> drawableRequestListener) {
        displayImage(getRequestBuilderDrawable(glide, requestUrl), null, transformation, null, drawableCustomTarget, drawableRequestListener);
    }


    /**
     **/
    public RequestBuilder<Drawable> getRequestBuilderDrawable(RequestManager glide, String requestUrl) {
        if (GlidePlaceHolderDrawable != null && GlideErrorDrawable != null) {
            return glide.load(requestUrl).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .placeholder(GlidePlaceHolderDrawable)
                    .error(GlideErrorDrawable);
        } else if (GlidePlaceHolderDrawable != null && GlideErrorDrawable == null) {
            return glide.load(requestUrl).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .placeholder(GlidePlaceHolderDrawable);
        } else if (GlidePlaceHolderDrawable == null && GlideErrorDrawable != null) {
            return glide.load(requestUrl).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .error(GlideErrorDrawable);
        } else {
            return glide.load(requestUrl).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        }
    }

    /**
     **/
    public RequestBuilder<Drawable> getRequestBuilderDrawable(RequestManager glide, int requestUrl) {
        if (GlidePlaceHolderDrawable != null && GlideErrorDrawable != null) {
            return glide.load(requestUrl).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .placeholder(GlidePlaceHolderDrawable)
                    .error(GlideErrorDrawable);
        } else if (GlidePlaceHolderDrawable != null && GlideErrorDrawable == null) {
            return glide.load(requestUrl).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .placeholder(GlidePlaceHolderDrawable);
        } else if (GlidePlaceHolderDrawable == null && GlideErrorDrawable != null) {
            return glide.load(requestUrl).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .error(GlideErrorDrawable);
        } else {
            return glide.load(requestUrl).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        }
    }

    /**
     **/
    public RequestBuilder<Drawable> getRequestBuilderDrawable(RequestManager glide, Uri uri) {
        if (GlidePlaceHolderDrawable != null && GlideErrorDrawable != null) {
            return glide.load(uri).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .placeholder(GlidePlaceHolderDrawable)
                    .error(GlideErrorDrawable);
        } else if (GlidePlaceHolderDrawable != null && GlideErrorDrawable == null) {
            return glide.load(uri).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .placeholder(GlidePlaceHolderDrawable);
        } else if (GlidePlaceHolderDrawable == null && GlideErrorDrawable != null) {
            return glide.load(uri).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .error(GlideErrorDrawable);
        } else {
            return glide.load(uri).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        }
    }

    /**
     **/
    public RequestBuilder<Drawable> getRequestBuilderDrawable(RequestManager glide, File file) {
        if (GlidePlaceHolderDrawable != null && GlideErrorDrawable != null) {
            return glide.load(file).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .placeholder(GlidePlaceHolderDrawable)
                    .error(GlideErrorDrawable);
        } else if (GlidePlaceHolderDrawable != null && GlideErrorDrawable == null) {
            return glide.load(file).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .placeholder(GlidePlaceHolderDrawable);
        } else if (GlidePlaceHolderDrawable == null && GlideErrorDrawable != null) {
            return glide.load(file).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .error(GlideErrorDrawable);
        } else {
            return glide.load(file).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        }
    }

    /**
     **/
    public RequestBuilder<Bitmap> getRequestBuilderBitmap(RequestManager glide, String requestUrl) {
        if (GlidePlaceHolderDrawable != null && GlideErrorDrawable != null) {
            return glide.asBitmap().load(requestUrl).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .placeholder(GlidePlaceHolderDrawable)
                    .error(GlideErrorDrawable);
        } else if (GlidePlaceHolderDrawable != null && GlideErrorDrawable == null) {
            return glide.asBitmap().load(requestUrl).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .placeholder(GlidePlaceHolderDrawable);
        } else if (GlidePlaceHolderDrawable == null && GlideErrorDrawable != null) {
            return glide.asBitmap().load(requestUrl).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .error(GlideErrorDrawable);
        } else {
            return glide.asBitmap().load(requestUrl).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        }
    }

    /**
     **/
    public RequestBuilder<Bitmap> getRequestBuilderBitmap(RequestManager glide, int requestUrl) {
        if (GlidePlaceHolderDrawable != null && GlideErrorDrawable != null) {
            return glide.asBitmap().load(requestUrl).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .placeholder(GlidePlaceHolderDrawable)
                    .error(GlideErrorDrawable);
        } else if (GlidePlaceHolderDrawable != null && GlideErrorDrawable == null) {
            return glide.asBitmap().load(requestUrl).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .placeholder(GlidePlaceHolderDrawable);
        } else if (GlidePlaceHolderDrawable == null && GlideErrorDrawable != null) {
            return glide.asBitmap().load(requestUrl).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .error(GlideErrorDrawable);
        } else {
            return glide.asBitmap().load(requestUrl).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        }
    }


    /**
     **/
    private void displayImageAsBitmap(RequestBuilder<Bitmap> bitmapRequestBuilder, ImageView imageView, RequestListener<Bitmap> bitmapRequestListener, Transformation<Bitmap>... transformations) {
        if (transformations != null) {
            bitmapRequestBuilder.transform(transformations);
        }

        if (bitmapRequestListener != null) {
            bitmapRequestBuilder.listener(bitmapRequestListener);
        }

        if (imageView != null) {
            bitmapRequestBuilder.into(imageView);
        }
    }


    /**
     **/
    private void displayImage(RequestBuilder<Drawable> drawableRequestBuilder, ImageView imageView, Transformation transformation, RequestOptions requestOptions, CustomTarget<Drawable> drawableCustomTarget, RequestListener<Drawable> drawableRequestListener) {
        if (imageView != null) {
            if (transformation != null) {
                drawableRequestBuilder.transform(transformation);
            } else if (requestOptions != null) {
                drawableRequestBuilder.apply(requestOptions);
            }

            if (drawableRequestListener != null) {
                drawableRequestBuilder.listener(drawableRequestListener);
            }

            drawableRequestBuilder.into(imageView);
        } else if (drawableCustomTarget != null) {
            drawableRequestBuilder.into(drawableCustomTarget);
        }
    }

    private void displayImageAsBitmap(RequestBuilder<Bitmap> bitmapRequestBuilder, ImageView imageView, Transformation transformation, RequestOptions requestOptions, CustomTarget<Bitmap> bitmapCustomTarget, RequestListener<Bitmap> bitmapRequestListener) {
        if (requestOptions != null) {
            bitmapRequestBuilder.apply(requestOptions);
        } else if (transformation != null) {
            bitmapRequestBuilder.transform(transformation);
        }

        if (bitmapRequestListener != null) {
            bitmapRequestBuilder.listener(bitmapRequestListener);
        }

        if (imageView != null) {
            bitmapRequestBuilder.into(imageView);
        } else if (bitmapCustomTarget != null) {
            bitmapRequestBuilder.into(bitmapCustomTarget);
        }
    }


    public Drawable getGlidePlaceHolderDrawable() {
        return GlidePlaceHolderDrawable;
    }

    public Drawable getGlideErrorDrawable() {
        return GlideErrorDrawable;
    }

}
